<?php
if( !defined('IN') ) die('bad request');
include_once( AROOT . 'controller'.DS.'app.class.php' );

class testController extends appController
{
	function __construct()
	{
		parent::__construct();
		$this->check_login();
	}
	
	function index()
	{
		
		global $db_table_prefix;
		$data['title'] = $data['top_title'] = "测试";
		$data['outVar']=array();
		$data['html_data']= '';

		$data['outVar']['nav_src']=user_nav_html('test');
		
		$mode=v('mode');

		$src = '';
		if(empty($mode)){
			//only index
		}else{
			$mode = intval($mode);

			if($mode<1){
				$src .= '参数无效！';
			}else{
				$ret_data =self::init_test($mode);
				$src .= $ret_data['msg'];
				if(isset($ret_data['log'])){
					$src .= str_ireplace(PHP_EOL, '<br/>', $ret_data['log']) ;
				}
			}
		}		
		$data['html_data']= $src;
		return render( $data , 'web' , 'test.index' );
	}

	public static function init_test($id)
	{	
		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');	

		switch ($id) {
			case 1:
				$ret_data['msg']='初始化100条测试用地图移动事件！';
				$table = real_table_name('event_info');
				$cf_now = date('Y-m-d H:i:s');
				$tmpSqlTpl="INSERT INTO `".$table."` (`ffuid`, `title`, `desc`, `is_his`, `category`, `sub_category`, `create_date`, `modify_date`) VALUES ('10000', ?s, ?s, '0', '2', '2', ?s, ?s);";
				$tmpid = substr( md5( time().rand(1,9999) ) , rand( 1 , 20 ) , 12   );
				for ($i=0; $i < 100; $i++) { 
					$desc = '地图移动测试事件'.$tmpid.'#'.$i;
					$tmpSql = prepare($tmpSqlTpl ,array('title.'.$desc,'desc.'.$desc.$desc,$cf_now,$cf_now) );
					run_sql($tmpSql);
				}
				break;

			case 2:
				return self::init_event_info_for_test('登陆奖励',1,1,50);
				break;

			case 3:
				return self::init_map_block_info_for_test();
				break;
			
			case 4:
				return self::init_event_info_for_test('探索随机事件',2,3,50);
				break;

			case 5:
				return self::init_event_info_for_test('NPC随机事件',2,4,30);
				break;

			case 6:
				return self::init_event_info_for_test('营地随机事件',2,1,30);
				break;

			case 7:
				return self::init_event_info_for_test('环境随机事件',2,5,30);
				break;

			case 8:
				return self::init_event_info_for_test('GM奖励',1,2,30);
				break;

			case 9:
				return self::init_event_info_for_test('系统任务',1,3,50);
				break;

			case 10:
				return self::init_event_info_for_test('玩家任务',1,4,30);
				break;

			case 11:
				return self::reset_map_block_id_for_test();
				break;

			// case 12:
			// 	return self::init_item_from_cdda();
			// 	break;
				

			default:
				$ret_data['msg']='无效的参数！';
				$ret_data['err_code']=1;
				break;
		}
		
		return $ret_data;	
	}

	public static function init_event_info_for_test($msg,$cat1,$cat2,$cout)
	{	
		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');	
		$c1 = intval($cat1);
		$c2 = intval($cat2);
		$c = intval($cout);
		if(!empty($msg) && $c1>0 && $c2>0 && $c>0){
			$ret_data['msg']='初始化'.$c.'条['.$msg.']事件记录！';
			$ret_data['log']='';
			$table = real_table_name('event_info');
			$cf_now = date('Y-m-d H:i:s');
			$tmpSqlTpl="INSERT INTO `".$table."` (`ffuid`, `title`, `desc`, `is_his`, `category`, `sub_category`, `create_date`, `modify_date`) VALUES ('10000', ?s, ?s, '0', ?s, ?s, ?s, ?s);";
			$tmpid = substr( md5( time().rand(1,9999) ) , rand( 1 , 20 ) , 12   );
			for ($i=0; $i < $c; $i++) { 
				$desc = $msg.$tmpid.'#'.$i;
				$tmpSql = prepare($tmpSqlTpl ,array('title.'.$desc,'desc.'.$desc.$desc,$c1,$c2,$cf_now,$cf_now) );
				run_sql($tmpSql);
				$ret_data['log'] .= $desc.PHP_EOL;
			}
		}else{
			$ret_data['msg']='无效的参数！';
			$ret_data['err_code']=1;
		}
	
		return $ret_data;	
	}

	public static function init_map_block_info_for_test()
	{	
$txt_buff='id;title;desc;enter_item_id;loot_set_id;door_num;door_pos;path_pos;parent_id;has_child
100;居民楼n1;待补充;0;1;1;1;0;0;0
101;居民楼n2;待补充;0;1;1;1;0;0;0
102;居民楼n3;待补充;0;1;1;1;0;0;0
103;居民楼n4;待补充;0;1;1;1;0;0;0
104;居民楼e1;待补充;0;1;1;2;0;0;0
105;居民楼e2;待补充;0;1;1;2;0;0;0
106;居民楼e3;待补充;0;1;1;2;0;0;0
107;居民楼e4;待补充;0;1;1;2;0;0;0
108;居民楼s1;待补充;0;1;1;3;0;0;0
109;居民楼s2;待补充;0;1;1;3;0;0;0
110;居民楼s3;待补充;0;1;1;3;0;0;0
111;居民楼s4;待补充;0;1;1;3;0;0;0
112;居民楼w1;待补充;0;1;1;4;0;0;0
113;居民楼w2;待补充;0;1;1;4;0;0;0
114;居民楼w3;待补充;0;1;1;4;0;0;0
115;居民楼w4;待补充;0;1;1;4;0;0;0
116;停车场n1;待补充;0;2;1;1;0;0;0
117;停车场e1;待补充;0;2;1;2;0;0;0
118;停车场s1;待补充;0;2;1;3;0;0;0
119;停车场w1;待补充;0;2;1;4;0;0;0
120;十字路口;待补充;0;3;4;1234;1234;0;0
121;丁字路口1;待补充;0;3;3;234;234;0;0
122;丁字路口2;待补充;0;3;3;341;341;0;0
123;丁字路口3;待补充;0;3;3;412;412;0;0
124;丁字路口4;待补充;0;3;3;123;123;0;0
125;公路1;待补充;0;2;2;13;13;0;0
126;公路2;待补充;0;2;2;24;24;0;0
127;配电站;待补充;10;3;1;1;0;0;0
128;银行;待补充;0;4;1;3;0;0;0
129;警察局;待补充;0;5;1;3;0;0;0
130;社区诊所;待补充;0;6;1;3;0;0;0
131;中心医院;待补充;0;6;1;3;0;0;0
132;公园;待补充;0;7;1;3;0;0;0
133;绿地;待补充;0;7;1;3;0;0;0
134;地铁站;待补充;0;8;1;3;0;0;0
135;派出所;待补充;0;5;1;3;0;0;0
136;大剧院;待补充;0;9;1;3;0;0;0
137;快捷酒店;待补充;0;10;1;3;0;0;0
138;五金商店;待补充;0;11;1;3;0;0;0
139;电影院;待补充;0;9;1;3;0;0;0
140;商务大厦;待补充;0;12;1;3;0;0;0';

//INSERT INTO `shdic_sc2015_map_block_info`(`id`, `title`, `info_desc`, `code`, `is_his`, `category`, `thumbnail_id`, `path_pos`, `enter_item_id`, `loot_set_id`, `door_num`, `door_pos`, `has_child`, `bg_img`, `ca_x`, `ca_y`, `ca_z`, `create_date`, `modify_date`, `parent_id`, `sort`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],[value-11],[value-12],[value-13],[value-14],[value-15],[value-16],[value-17],[value-18],[value-19],[value-20],[value-21])
		$table = real_table_name('map_block_info');
		$cf_now = date('Y-m-d H:i:s');
		$tmpSqlTpl="INSERT INTO `".$table."` (`id`, `title`, `info_desc`, `code`, `is_his`, `category`, `thumbnail_id`, `path_pos`, `enter_item_id`, `loot_set_id`, `door_num`, `door_pos`, `has_child`, `bg_img`, `ca_x`, `ca_y`, `ca_z`, `create_date`, `modify_date`, `parent_id`, `sort`) VALUES (?i, ?s, ?s, ?s,0,0,0, ?s, ?s, ?s, ?s, ?s, ?s, ?s, 0, 0, 0, ?s, ?s, ?i, ?i);";
		
//id;title;desc;enter_item_id;loot_set_id;door_num;door_pos;path_pos;parent_id;has_child

		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');	
		$itm_buff = null;
		$msg='';
		$arr = explode("\n", $txt_buff);
		foreach ($arr as $lid => $line) {
			if(strlen($line)>5){
				$cell = explode(';', $line);
				if(count($cell)>3){
					if($lid==0){
						$itm_buff=array();
						foreach ($cell as $k => $v) {
							$itm_buff[$v]=$k;
						}
						// $msg .= 'Title:'. print_r($itm_buff,true) . '<hr/>';
					}else{
						$new_itm=array();
						foreach ($itm_buff as $k => $v) {
							$new_itm[$k]=$v;							
							foreach ($cell as $k2 => $v2) {								
								if($v==$k2){
									$msg .= 'k='.$k.',v='.$v;
									$msg .= 'check k2='.$k2.',v2='.$v2. '<hr/>';
									$new_itm[$k] =$v2;
								}
							}
						}
						$msg .= 'Item:'. print_r($new_itm,true). '<hr/>';

						// $code = kis_uuid();				
						//最后一个字母应该是不为0的数字
						$new_itm['title'] = trim($new_itm['title']);
						$tt = $new_itm['title'];			
						$tt = substr($tt,strlen($tt)-1,1);
						if(intval($tt)<1){
							$new_itm['title'] = $new_itm['title'] . '1';
						}
						$code = md5($new_itm['title']);

						$tmpSql = prepare($tmpSqlTpl ,array($new_itm['id'],$new_itm['title'],$new_itm['info_desc'],$code,$new_itm['path_pos'],$new_itm['enter_item_id'],$new_itm['loot_set_id'],$new_itm['door_num'],$new_itm['door_pos'],$new_itm['has_child'],'',$cf_now,$cf_now,0,0) );
						run_sql($tmpSql);

						$msg .= $tmpSql;

					}
				}
			}
		}

		$ret_data['msg']=$msg;
		return $ret_data;	
	}



	public static function reset_map_block_id_for_test()
	{	
		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');
		$msg='';
		$table = real_table_name('map_block_info');
//SELECT * FROM `shdic_sc2015_map_block_info` WHERE 1
		$tmpSql="SELECT id FROM `".$table."` WHERE  `is_his`=0 AND  `pos_x` !=1000 AND  `pos_x` !=1000 ";
		$msg .= 'sql1='.$tmpSql.PHP_EOL;
		$mb_data = get_data($tmpSql);
		if( $mb_data==false ){
			$ret_data['err_code']=1;
			$ret_data['msg']=$msg;
			return $ret_data;
		}

		$count=count($mb_data);
		$msg .= 'count='.$count.PHP_EOL;
		$ii=0;
		$ids_buff=array();
		foreach ($mb_data as $key => $value) {
			$ids_buff[]=$value['id'];
			$ii ++;
		}
		unset($mb_data) ;	
		$msg .= 'ii='.$ii.PHP_EOL;	

//SELECT `id`, `title`, `code`, `is_his`, `category`, `thumbnail_id`, `block_id`, `path_id`, `door_id`, `bg_img`, `pos_x`, `pos_y`, `ud1`, `ud2`, `ud3`, `ud4`, `ud5`, `ud6`, `ud7`, `ud8`, `ud9`, `create_date`, `modify_date`, `parent_id`, `sort` FROM `shdic_sc2015_map_info` WHERE 1

		$cf_now = date('Y-m-d H:i:s');
		$kv=kget(__FUNCTION__);
		if(empty($kv)){
			$kv=$cf_now;
			$msg .= '<hr>is New'.$kv.'<br/>'.PHP_EOL;
		}else{
			if( substr($cf_now, 0,10) ==substr($kv,0,10) ){
				//continue
				$msg .= '<hr>is continue'.$kv.'<br/>'.PHP_EOL;
			}else{
				$kv=$cf_now;
				$msg .= '<hr>Reset New'.$kv.'<br/>'.PHP_EOL;
			}
		}
		kset(__FUNCTION__,$kv);

		$table = real_table_name('map_info');
		$tmpSql = prepare("SELECT * FROM `".$table."` WHERE  `is_his`=0 AND `modify_date`<>?s limit 500 " ,array($kv) );
		$msg .= 'sql2='.$tmpSql.PHP_EOL;

		if( $tmp_data = get_data($tmpSql) ){		
			$iii=0;
			foreach ($tmp_data as $key => $value) {
				$rand=_rnd(0,$count);
				$iii++;
				$mb_id = $ids_buff[$rand];
				$id = $value['id'];
				$tmpSql = prepare("UPDATE `".$table."` SET `block_id`=?s , `modify_date`=?s WHERE  `is_his`=0 AND `id`=?s " ,array($mb_id,$kv,$id) );
				$msg .= '#'.$iii.',sql='.$tmpSql.'<br/>'.PHP_EOL;
				run_sql($tmpSql);
			}
		}


	//TODO
	//ca_x 周边8格子里面，最多出现几次
	//ca_y 周边8格子里面，最少出现几次
	//ca_z ...
		$ret_data['msg']=$msg;
		return $ret_data;	
	}


	public static function init_item_from_cdda()
	{	
		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');
		$msg='';
		$table = real_table_name('item_info');

		$tpl_sql ="INSERT INTO `".$table."`(`ffuid`, `title`, `desc`,`is_his`, `category`, `sub_category`,`max_stack`, `single_weight`, `quality`, `num1`, `num2`, `durable`, `create_date`, `modify_date`) VALUES (?i,?s,?s,?i,?i,?i,?s,?i,?i,?i,?i,?i,?s,?s)";
		
		$uid = 0;
		$cat1 = 0;
		$cat2 = 0;
		$weight = 1;
		$quality = 1;
		$num1 = 1;
		$num2 = 2;
		$stack = 1;
		$durable = 1;
		$cf_now = date('Y-m-d H:i:s');

		$fname='E:\kevinshen1021\exe\devUSBWebser_8.6\root\dev-test\cataclysm-dda.html';
		$src = file_get_contents($fname);
		$cdda = explode('</tr>', $src);
		$bg1="bgcolor='#FFFFFF'";
		$bg2="bgcolor='#F2F2F2'";

		$line_id = 0;
		foreach ($cdda as $key => $value) {
			$line='';
			if($line_id>5000+$fc){
				// break;
			}
			if(stripos($value, $bg1)!=false || stripos($value, $bg2)!=false){
				$line_id ++;		
				$line = $value.'</tr>';
				$tt_arr = explode('</td>', $line);
				if(count($tt_arr)>1){
					$tt = $tt_arr[1];
					$tt = self::get_txt($tt);
					if($line_id>$fc){
						// echo '#'.$line_id.','.$tt.'<hr/>'.PHP_EOL;	
						$itmName = 'CDDA_txt#'.$line_id;
						$itmDesc = $tt;
						$tmp_sql=prepare($tpl_sql,array($uid,$itmName,$itmDesc,0,$cat1,$cat2,$stack,$weight,$quality,$num1,$num2,$durable,$cf_now,$cf_now));
						run_sql($tmp_sql);
					}
					
				}
			}else{
				// echo '.';
			}

		}	//end foreach
	
		$ret_data['msg']=$msg;
		return $ret_data;	
	}

	public static function get_txt($tt){
		$tt = str_ireplace('\n', '', $tt);
		$tt = str_ireplace('<td>', '', $tt);
		$tt = str_ireplace(PHP_EOL, '', $tt);
		$tt = str_ireplace(' ', '', $tt);
		return $tt;
	}


}